﻿app.controller('AdminController', function ($scope, $http, $window, $compile, $filter, ipCookie, common, option) {
    $scope.result = {};
    $scope.dateOption = option.date;
    $scope.datetimeOption = option.datetime;

    $scope.IsAdmin = 1;

    $scope.GoToPage = function (name, id, page) {
        location.href = name + "?id=" + id + "&page=" + page;
    }

    $scope.GoPage = function (name, page) {
        location.href = name + "?&page=" + page;
    }

    $scope.AdminLogin = function (user, psd) {
        $http({
            method: 'POST',
            url: '../Admin/AdminLogin',
            data: { 'userName': user, 'password': psd }
        })
            .success(function (ret) {
                if (ret.Code == 100) {
                    window.location = "UserList";
                }
                else {
                    alertify.alert(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {

            });
    }

    $scope.GetManagerUserList = function (searchmodel, page) {
        $scope.ManagerUserLoading = true;
        $http({
            method: 'POST',
            url: '../Admin/GetUserList',
            data: { 'model': searchmodel },
            params: { 'page': page }
        })
            .success(function (ret) {
                common.success($scope, null, ret);
                if (ret.Code == 100) {
                    $scope.TotalItemCount = ret.Data.TotalItemCount;
                    $scope.ManagerUserList = ret.Data.Data;
                    $scope.ManagerUserLoading = false;
                }
                else {
                    alertify.alert(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {

            });
    }
    $scope.GetCompanyList = function () {
        $scope.PageLoading = true;
        $scope.TypeList = [];
        $http({
            method: 'POST',
            url: '../HiddenTrouble/GetCompanyList',
        })
            .success(function (ret) {
                common.success($scope, null, ret);
                if (ret.Code == 100) {
                    $scope.CompanyList = ret.Data;
                    $scope.PageLoading = false;
                }
                else {
                    alertify.alert(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {

            });
    }
    $scope.GetheckOutList = function (searchmodel, page) {
        $scope.PageLoading = true;
        searchmodel.StartDate = $("#StartDate").val();
        searchmodel.EndDate = $("#EndTime").val();
        $http({
            method: 'POST',
            url: '../Admin/GetheckOutList',
            data: { 'model': searchmodel },
            params: { 'page': page ,'pageSize':70}
        })
            .success(function (ret) {
                common.success($scope, null, ret);
                if (ret.Code == 100) {
                    $scope.TotalItemCount = ret.Data.TotalItemCount;
                    $scope.CheckOutList = ret.Data.Data;
                    $scope.PageLoading = false;
                    $scope.Sum = ret.Msg;
                }
                else {
                    alertify.alert(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {

            });
    }

    $scope.ExportCheckOut = function (searchmodel) {
        searchmodel.StartDate = $("#StartDate").val();
        searchmodel.EndDate = $("#EndTime").val();
        $http({
            method: 'POST',
            url: '../Admin/ExportCheckOutList',
            data: { 'model': searchmodel}
        })
            .success(function (ret) {
                common.success($scope, null, ret);
                if (ret != "") {
                    window.location.href = ret;
                }
                else {
                    alertify.alert("导出错误");
                }
            })
            .error(function (data, status, headers, config) {

            });
    }

    $scope.GetSalesReportList = function (searchmodel, page) {
        if ($('#chk_companygroup').is(':checked')) {
            if (searchmodel.GoodsText == "" || searchmodel.GoodsText == null) {
                alertify.alert("必须选择商品在进行分组查询！");
                return false;
            }
            $scope.Type = 2;
        }
        else {
            $scope.Type =1;
        }
        $scope.PageLoading = true;
        if ($scope.Type == 1) {
            $http({
                method: 'POST',
                url: '../SalesReport/GetSalesReportList',
                data: { 'model': searchmodel },
                params: { 'page': page, 'pageSize': 80 }
            })
                .success(function (ret) {
                    common.success($scope, null, ret);
                    if (ret.Code == 100) {
                        $scope.TotalItemCount = ret.Data.TotalItemCount;
                        $scope.Sum = ret.Msg;
                        $scope.SalesReportList = ret.Data.Data;
                        $scope.PageLoading = false;
                    }
                    else {
                        alertify.alert(ret.Msg);
                    }
                })
                .error(function (data, status, headers, config) {

                });
        }
        else {
            
            $http({
                method: 'POST',
                url: '../SalesReport/GetSalesReportListByCompany',
                data: { 'model': searchmodel },
                params: { 'page': page, 'pageSize': 80 }
            })
                .success(function (ret) {
                    common.success($scope, null, ret);
                    if (ret.Code == 100) {
                        $scope.TotalItemCount = ret.Data.TotalItemCount;
                        $scope.Sum = ret.Msg;
                        $scope.SalesReportListByCompany = ret.Data;
                        $scope.PageLoading = false;
                    }
                    else {
                        alertify.alert(ret.Msg);
                    }
                })
                .error(function (data, status, headers, config) {

                });

        }

    }



});

app.filter('HideCardNo', function () { //可以注入依赖
    return function (text) {
        if (text != null && text != "")
            return text.replace(/^(.{3})(?:\d+)(.{4})$/, "$1******$2");
    }
});

app.filter('HideText', function () { //可以注入依赖
    return function (text) {
        if (text.length > 7) {
            return text.substring(0, 8) + "...";
        }
        else {
            return text;
        }
    }
});

app.filter('IsExist', function () { //可以注入依赖
    return function (text) {
        if (text)
            return "可用";
        else
            return "禁用";
    }
});